//
//  SEGPayToolView.m
//  ExcellentHome
//
//  Created by 刘山 on 16/12/13.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGPayToolView.h"

@interface SEGPayToolView()

@property (nonatomic, strong) UIButton *radioButton;

@end

@implementation SEGPayToolView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    [self addSubview:self.contentLabel];// 共计费用
    self.contentLabel.numberOfLines = 0;
    
    [self addSubview:self.contentLabel1];// 优惠金额
    self.contentLabel1.numberOfLines = 0;
    [self.contentLabel1 setFont:[UIFont systemFontOfSize:10.f]];
    [self.contentLabel1 setTextColor:kColor_1];
    
    [self addSubview:self.contentLabel2];// 优惠金额
    self.contentLabel2.numberOfLines = 0;
    [self.contentLabel2 setFont:kFont_10];
    [self.contentLabel2 setTextColor:kColor_3];
    self.contentLabel2.text = kNeedBillDesc;
    
    self.backgroundColor = kColor_W;
    
    [self addSubview:self.titleBtn];
    [self.titleBtn setTitleColor:kColor_W forState:UIControlStateNormal];
    [self.titleBtn setBackgroundImage:[UIImage imageWithColor:kColor_S1] forState:UIControlStateNormal];
    [self.titleBtn setBackgroundImage:[UIImage imageWithColor:kColor_L2] forState:UIControlStateDisabled];
    [self.titleBtn.titleLabel setFont:kFont_16];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.height.mas_equalTo(0.5);
        make.width.equalTo(self);
    }];
}

-(void)setShowType:(NSString *)showType{
    _showType = showType;
    
    [self.titleBtn mas_makeConstraints:^(MASConstraintMaker* make) {
        if (LYIsiPhoneX()) {
            make.right.mas_equalTo(-15);
            make.top.mas_equalTo(5);
            make.bottom.mas_equalTo(-5);
            make.width.mas_equalTo(115);
        } else {
            make.top.right.bottom.mas_equalTo(0);
            make.width.mas_equalTo(LYScreenWidth() == 320 ? 80 : 120);
        }
    }];
    
    if ([_showType isEqualToString:@"1"]) {
        [self.contentLabel2 setHidden:YES];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.left.equalTo(self).mas_offset(15);
            make.right.equalTo(self.titleBtn.mas_left).offset(-15);
            make.centerY.equalTo(self);
        }];
        
        [self.contentLabel1 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.right.equalTo(self.titleBtn.mas_left).mas_offset(-5);
            make.centerY.equalTo(self);
            make.left.equalTo(self.contentLabel.mas_right).mas_offset(5);
        }];
    }else{
        [self.contentLabel2 setHidden:NO];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
            make.left.equalTo(self).mas_offset(15);
            make.right.equalTo(self.titleBtn.mas_left).offset(-15);
            make.centerY.equalTo(self).multipliedBy(0.6);
        }];
        
        [self.contentLabel1 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.right.equalTo(self.titleBtn.mas_left).mas_offset(-5);
            make.bottom.equalTo(self.contentLabel);
            make.left.equalTo(self.contentLabel.mas_right).mas_offset(5);
        }];
        
        [self.contentLabel2 sizeToFit];
        [self.contentLabel2 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.left.equalTo(self.contentLabel.mas_left);
            make.right.equalTo(self.contentLabel.mas_right);
            make.top.equalTo(self.contentLabel.mas_bottom).mas_offset(5);
        }];
    }
}

- (void)showRadioButton {
    [self addSubview:self.radioButton];
    self.radioButton.hidden = NO;
    [self.radioButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.top.left.bottom.mas_equalTo(0);
    }];
    
    if ([_showType isEqualToString:@"1"]) {
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(80);
            make.right.equalTo(self.titleBtn.mas_left).offset(-15);
            make.centerY.equalTo(self);
        }];
    } else {
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
            make.left.mas_equalTo(80);
            make.right.equalTo(self.titleBtn.mas_left).offset(-15);
            make.centerY.equalTo(self).multipliedBy(0.6);
        }];
    }
}

- (void)hideRadioButton {
    self.radioButton.hidden = YES;
    
    if ([_showType isEqualToString:@"1"]) {
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.equalTo(self.titleBtn.mas_left).offset(-15);
            make.centerY.equalTo(self);
        }];
    } else {
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
            make.left.mas_equalTo(15);
            make.right.equalTo(self.titleBtn.mas_left).offset(-15);
            make.centerY.equalTo(self).multipliedBy(0.6);
        }];
    }
}

- (void)setRadioButtonSelected:(BOOL)isSelected {
    self.radioButton.selected = isSelected;
}

#pragma mark - action
- (void)radioButtonSelect:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.radioSelectBlock) {
        self.radioSelectBlock(sender.isSelected);
    }
}

#pragma mark - lazy load
- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
    }
    return _contentLabel;
}

- (UILabel *)contentLabel1 {
    if (!_contentLabel1) {
        _contentLabel1 = [[UILabel alloc] init];
    }
    return _contentLabel1;
}

- (UILabel *)contentLabel2 {
    if (!_contentLabel2) {
        _contentLabel2 = [[UILabel alloc] init];
    }
    return _contentLabel2;
}

- (UIButton *)titleBtn {
    if (!_titleBtn) {
        _titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (LYIsiPhoneX()) {
            _titleBtn.layer.cornerRadius = 4;
            _titleBtn.layer.masksToBounds = YES;
        }
    }
    return _titleBtn;
}

- (UIButton *)radioButton {
    if (!_radioButton) {
        _radioButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_radioButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select_nor")] forState:UIControlStateNormal];
        [_radioButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")] forState:UIControlStateSelected];
        [_radioButton setTitle:@"全选" forState:UIControlStateNormal];
        [_radioButton setTitleColor:kColor_1 forState:UIControlStateNormal];
        _radioButton.titleLabel.font = kFont_15;
        _radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _radioButton.imageEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        _radioButton.titleEdgeInsets = UIEdgeInsetsMake(0, 21, 0, 0);
        [_radioButton addTarget:self action:@selector(radioButtonSelect:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _radioButton;
}

@end
